package com.zklh.leetcode.lcp;

/**
 * 最多可以摧毁的敌人城堡数目
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/9/15
 */
public class Solution2511 {
    public int captureForts(int[] forts) {
        int nobodyIndex = 0;
        int maxDestroyFoe = 0;
        int myIndex = 0;
        while (nobodyIndex < forts.length) {
            while (nobodyIndex < forts.length && forts[nobodyIndex] != -1 ) nobodyIndex++;
            if (nobodyIndex == forts.length) {
                break;
            }
            myIndex = nobodyIndex - 1;
            while (myIndex >= 0 && forts[myIndex] == 0) myIndex--;
            if (myIndex >= 0 && forts[myIndex] == 1) {
                maxDestroyFoe = Integer.max(maxDestroyFoe, Math.abs(nobodyIndex-myIndex) - 1);
            }
            myIndex = nobodyIndex + 1;
            while (myIndex < forts.length && forts[myIndex] == 0) myIndex++;
            if (myIndex < forts.length && forts[myIndex] == 1) {
                maxDestroyFoe = Integer.max(maxDestroyFoe, Math.abs(nobodyIndex-myIndex) - 1);
            }
            nobodyIndex++;
        }
        return maxDestroyFoe;
    }
}